import * as fs from "fs";
import { mkdir } from "fs/promises";
import { KOA_STATIC_CACHE_PATH } from "../config/const";
import * as path from "path";
class _LSFS {

    /**
     * 
     * @param _path string
     * @returns //是否是文件夹，是否存在
     */
    isDir(_path: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            try {
                fs.stat(_path, (err, state) => {
                    if(err){
                        resolve(false);
                    } else {
                        resolve(state.isDirectory())
                    }
                     
                })
            } catch (err) {
                console.log("错误:", err);
                //报错也是true，也代表已经存在
                // reject(true)
            }
        })
    }

    async createDirItems(name: string,_path: string = KOA_STATIC_CACHE_PATH) {
        const PATH = path.resolve(_path, name)
        return new Promise(async (resolve, reject) => {
            try {
                //创建剧本名字的目录
              await mkdir(PATH);
              // 创建角色，线索，封面的目录
              await mkdir(path.resolve(PATH,"封面"))
              await mkdir(path.resolve(PATH,"线索"))
              await mkdir(path.resolve(PATH,"角色"))
              resolve(true)
            } catch (err) {
                console.log("创建目录失败:", err);
                // reject(false)
                resolve(false)
            }
        })
         
      
       
    }
}

const LSFS = new _LSFS();

export {
    LSFS
}